
/*code 'foreigner' treatment*/

tab treat
encode treat, gen(treats)
tab treats
tab treats, nolabel

gen foreigner=.
recode foreigner(.=0) if treats==1
recode foreigner(.=1) if treats==2
tab foreigner

drop if foreigner==.


/*sample characteristics*/

tab ancest, nolabel

encode Q11, gen(edu)
tab edu
recode edu(5=.)
tab edu

encode Q10, gen(liberal)
tab liberal
recode liberal(8=.)
summ liberal

encode Q8, gen(usborn)
tab usborn
recode usborn(3=.)
tab usborn

encode Q7, gen(female)
tab female
recode female(4=.)
tab female

encode Q6, gen(age1)
tab age1, nolabel
gen ages=(age1+17)
summ ages

summ edu, detail
summ liberal, detail
tab usborn
tab female
summ ages

tab usborn
tab female
tab edu

recode usborn(2=0)
tab usborn, nolabel

recode female(2=0)
recode female(3=0)
tab female, nolabel


/*balance check*/

ttest ages, by(foreigner)
ttest liberal, by(foreigner)
tab edu foreigner, chi2
tab usborn foreigner, chi2
tab female foreigner, chi2

reg foreigner ages liberal edu usborn female
test ages liberal edu usborn female


/*manipulation check, drop those who failed it*/

encode Q17, gen(checks)
tab checks
tab checks, nolabel

gen check=.
recode check(.=1) if checks==1
recode check(.=0) if checks==2
tab check

drop if check==0


/*code mediator*/

encode Q19, gen(solids1)
tab solids1
tab solids1, nolabel
gen solid1=solids1
recode solid1 (6=.)
summ solid1

encode Q20, gen(solids2)
tab solids2
tab solids2, nolabel
gen solid2=solids2
recode solid2 (6=.)
recode solid2(1=5)(2=4)(3=3)(4=2)(5=1)
summ solid2

**check inter-item correlation

alpha solid1 solid2, std

gen solidscale=(solid1+solid2)/2
summ solidscale


/*code outcomes*/

**undocumented latino immigration, where higher values = more flexible views toward policies

encode Q22, gen (border1)
tab border1, nolabel
recode border1(6=.)
recode border1(1=5)(2=4)(3=3)(4=2)(5=1)
summ border1

encode Q23, gen(path1)
tab path1, nolabel
recode path1(6=.)
summ path1

encode Q24, gen(nobenefs1)
recode nobenefs1(6=.)
recode nobenefs1(1=5)(2=4)(3=3)(4=2)(5=1)
summ nobenefs1

summ border1 path1 nobenefs1

pwcorr border1 path1 nobenefs1, sig
alpha border1 path1 nobenefs1, std

**anti-terror policies, where higher values reflect more opposition to policies

encode Q26, gen(singleme1)
recode singleme1(6=.)
summ singleme1
recode singleme1(1=5)(2=4)(3=3)(4=2)(5=1)
summ singleme1

encode Q27, gen(surveils1)
recode surveils1 (6=.)
recode surveils1 (1=5)(2=4)(3=3)(4=2)(5=1)
summ surveils1

encode Q28, gen(visasme1)
recode visasme1 (6=.)
recode visasme1 (1=5)(2=4)(3=3)(4=2)(5=1)
summ visasme1

summ singleme1 surveils1 visasme1
alpha singleme1 surveils1 visasme1, std


**stereotypes, where higher values = greater endorsement

encode Q30_1, gen(MENAviolent)
recode MENAviolent(8=.)
summ MENAviolent

encode Q31_1, gen(MENAextreme)
recode MENAextreme(8=.)
summ MENAextreme

pwcorr MENAviolent MENAextreme, sig
gen MENAstereo=(MENAviolent+MENAextreme)/2

summ MENAstereo


encode Q33_1, gen(LATdumb)
recode LATdumb(8=.)(1=7)(2=6)(3=5)(4=4)(5=3)(6=2)(7=1)
summ LATdumb

encode Q34_1, gen(LATuntrust)
recode LATuntrust(8=.)(1=7)(2=6)(3=5)(4=4)(5=3)(6=2)(7=1)
summ LATuntrust

pwcorr LATdumb LATuntrust, sig
gen LATstereo=(LATdumb+LATuntrust)/2

summ LATstereo

pwcorr MENAstereo LATstereo, sig


/*mediation analyses*/

***rescale all vars to 0-1 range

gen solidscale2=(solidscale-1)/4
summ solidscale2

gen border2=(border1-1)/4
gen path2=(path1-1)/4
gen nobenefs2=(nobenefs1-1)/4
summ border2 path2 nobenefs2

gen singleme2=(singleme1-1)/4
gen surveils2=(surveils1-1)/4
gen visasme2=(visasme1-1)/4
summ singleme2 surveils2 visasme2

gen MENAstereo2=(MENAstereo-1)/6
gen LATstereo2=(LATstereo-1)/6
summ MENAstereo2 LATstereo2

*T to M

reg solidscale2 foreigner

***T to M with ideology as covariate

reg solidscale2 foreigner liberal

*T to O

reg border2 foreigner
reg path2 foreigner
reg nobenefs2 foreigner

reg singleme2 foreigner
reg surveils2 foreigner
reg visasme2 foreigner

reg MENAstereo2 foreigner
reg LATstereo2 foreigner

*M to O

reg border2 solidscale2 
reg path2 solidscale2
reg nobenefs2 solidscale2

reg singleme2 solidscale2
reg surveils2 solidscale2
reg visasme2 solidscale2

reg MENAstereo2 solidscale2
reg LATstereo2 solidscale2

***robust to including ideology in downstream path

reg border2 solidscale2 liberal
reg path2 solidscale2 liberal
reg nobenefs2 solidscale2 liberal
 
reg singleme2 solidscale2 liberal
reg surveils2 solidscale2 liberal 
reg visasme2 solidscale2 liberal

reg MENAstereo2 solidscale2 liberal
reg LATstereo2 solidscale2 liberal


***causal mediation anlaysis

medeff (regress solidscale2 foreigner) (regress border2 foreigner solidscale2), mediate(solidscale2) treat(foreigner) sims(1000) seed(1)
medeff (regress solidscale2 foreigner) (regress path2 foreigner solidscale2), mediate(solidscale2) treat(foreigner) sims(1000) seed(1)
medeff (regress solidscale2 foreigner) (regress nobenefs2 foreigner solidscale2), mediate(solidscale2) treat(foreigner) sims(1000) seed(1)

medeff (regress solidscale2 foreigner) (regress singleme2 foreigner solidscale2), mediate(solidscale2) treat(foreigner) sims(1000) seed(1)
medeff (regress solidscale2 foreigner) (regress surveils2 foreigner solidscale2), mediate(solidscale2) treat(foreigner) sims(1000) seed(1)
medeff (regress solidscale2 foreigner) (regress visasme2 foreigner solidscale2), mediate(solidscale2) treat(foreigner) sims(1000) seed(1)

medeff (regress solidscale2 foreigner) (regress MENAstereo2 foreigner solidscale2), mediate(solidscale2) treat(foreigner) sims(1000) seed(1)
medeff (regress solidscale2 foreigner) (regress LATstereo2 foreigner solidscale2), mediate(solidscale2) treat(foreigner) sims(1000) seed(1)



